package com.dimong.www.loan.ui.frag;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dimong.www.loan.R;
import com.dimong.www.loan.app.MyApplication;
import com.dimong.www.loan.db.CheckDao;
import com.dimong.www.loan.db.CheckModel;
import com.dimong.www.loan.db.CheckTotalDao;
import com.dimong.www.loan.db.CheckTotalModel;
import com.dimong.www.loan.ui.base.BaseTitleFragment;
import com.dimong.www.loan.widget.ActionBarView;
import com.j256.ormlite.stmt.query.In;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.qqtheme.framework.picker.DateTimePicker;
import cn.qqtheme.framework.util.DateUtils;

/**
 * Created by wuligang on 2018/5/29.
 */

public class AddCheckFrag extends BaseTitleFragment implements View.OnClickListener {
    private EditText money;
    private TextView time;
    private EditText address;

    private View travel;
    private View yule;
    private View daikuan;
    private View shenghuo;
    private View canyin;
    private View other;

    private View add_check;

    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;

    private CheckModel mCheckModel;

    public AddCheckFrag() {

    }

    @Override
    public RelativeLayout getContainView() {
        return null;
    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        money = (EditText) findViewById(R.id.money);
        time = (TextView) findViewById(R.id.time);
        address = (EditText) findViewById(R.id.address);
        travel = findViewById(R.id.travel);
        yule = findViewById(R.id.yule);
        daikuan = findViewById(R.id.daikuan);
        shenghuo = findViewById(R.id.shenghuo);
        canyin = findViewById(R.id.canyin);
        other = findViewById(R.id.other);

        add_check = findViewById(R.id.add_check);
    }

    @Override
    public void setViewStatus() {
        if (mTitle != null) {
            mTitle.setTitle("添加账单");
            mTitle.setLeftImg(new ActionBarView.OnLeftImgClickListener() {
                @Override
                public void onClick(View img) {
                    getActivity().finish();
                }
            });
        }

        travel.setOnClickListener(this);
        yule.setOnClickListener(this);
        daikuan.setOnClickListener(this);
        shenghuo.setOnClickListener(this);
        canyin.setOnClickListener(this);
        other.setOnClickListener(this);

        time.setOnClickListener(this);

        add_check.setOnClickListener(this);

        if (getArguments() != null) {
            mCheckModel = (CheckModel) getArguments().getSerializable("checkModel");
        }
        if (mCheckModel != null) {
            year = mCheckModel.getYear();
            month = mCheckModel.getMonth();
            day = mCheckModel.getDay();
            hour = mCheckModel.getHour();
            minute = mCheckModel.getMinute();
            if (mCheckModel.getType() == 0 || mCheckModel.getType() == 1) {
                travel.setSelected(true);
            }
            if (mCheckModel.getType() == 2) {
                yule.setSelected(true);
            }
            if (mCheckModel.getType() == 3) {
                daikuan.setSelected(true);
            }
            if (mCheckModel.getType() == 4) {
                shenghuo.setSelected(true);
            }
            if (mCheckModel.getType() == 5) {
                canyin.setSelected(true);
            }
            if (mCheckModel.getType() == 6) {
                other.setSelected(true);
            }
            money.setText(mCheckModel.getMoney() + "");
            if (!TextUtils.isEmpty(mCheckModel.getAddress())) {
                address.setText(mCheckModel.getAddress());
            }
        } else {
            Calendar curCalendar = Calendar.getInstance();
            year = curCalendar.get(Calendar.YEAR);
            month = curCalendar.get(Calendar.MONTH);
            day = curCalendar.get(Calendar.DATE);
            hour = curCalendar.get(Calendar.HOUR_OF_DAY);
            minute = curCalendar.get(Calendar.MINUTE);
            travel.setSelected(true);
        }
        setTime();
    }

    @Override
    public int getViewResourceId() {
        return R.layout.frag_add_check;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.travel:
                cancelSelect();
                travel.setSelected(true);
                break;
            case R.id.yule:
                cancelSelect();
                yule.setSelected(true);
                break;
            case R.id.daikuan:
                cancelSelect();
                daikuan.setSelected(true);
                break;
            case R.id.shenghuo:
                cancelSelect();
                shenghuo.setSelected(true);
                break;
            case R.id.canyin:
                cancelSelect();
                canyin.setSelected(true);
                break;
            case R.id.other:
                cancelSelect();
                other.setSelected(true);
                break;
            case R.id.time:
                Calendar curCalendar = Calendar.getInstance();
                DateTimePicker picker = new DateTimePicker(getActivity(), DateTimePicker.YEAR_MONTH_DAY);
                picker.setDateRangeStart(curCalendar.get(Calendar.YEAR) - 3, 1, 1);
                picker.setDateRangeEnd(curCalendar.get(Calendar.YEAR), 12, 31);
                picker.setTimeRangeStart(0, 0);
                picker.setTimeRangeEnd(23, 59);
                picker.setSelectedItem(curCalendar.get(Calendar.YEAR), curCalendar.get(Calendar.MONTH), curCalendar.get(Calendar.DATE), curCalendar.get(Calendar.HOUR_OF_DAY), curCalendar.get(Calendar.MINUTE));
                picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
                    @Override
                    public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                        AddCheckFrag.this.year = Integer.parseInt(year);
                        AddCheckFrag.this.month = Integer.parseInt(month);
                        AddCheckFrag.this.day = Integer.parseInt(day);
                        AddCheckFrag.this.hour = Integer.parseInt(hour);
                        AddCheckFrag.this.minute = Integer.parseInt(minute);

                        setTime();
                    }
                });
                picker.show();
                break;
            case R.id.add_check:
                addCheck();
                break;
        }
    }

    private void cancelSelect() {
        travel.setSelected(false);
        yule.setSelected(false);
        daikuan.setSelected(false);
        shenghuo.setSelected(false);
        canyin.setSelected(false);
        other.setSelected(false);
    }

    private int getSelectType() {
        if (travel.isSelected()) {
            return 1;
        }
        if (yule.isSelected()) {
            return 2;
        }
        if (daikuan.isSelected()) {
            return 3;
        }
        if (shenghuo.isSelected()) {
            return 4;
        }
        if (canyin.isSelected()) {
            return 5;
        }
        if (other.isSelected()) {
            return 6;
        }
        return 1;
    }

    private void setTime() {
        time.setText(year + "-" +
            DateUtils.fillZero(month) + "-" +
            DateUtils.fillZero(day) + "-" +
            DateUtils.fillZero(hour) + "-" +
            DateUtils.fillZero(minute));
    }

    private void addCheck() {
        if (TextUtils.isEmpty(money.getText().toString())) {
            showToast("金额不能为空");
            return;
        }
        CheckModel checkModel = new CheckModel();
        checkModel.setYear(year);
        checkModel.setMonth(month);
        checkModel.setDay(day);
        checkModel.setHour(hour);
        checkModel.setMinute(minute);
        if (mCheckModel != null) {
            checkModel.setId(mCheckModel.getId());
        }
        try {
            checkModel.setMoney(Float.parseFloat(money.getText().toString()));
        } catch (Exception e) {
            showToast("输入正确的金额");
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm");
        try {
            Date date = sdf.parse(time.getText().toString());
            checkModel.setTime(date.getTime());
        } catch (Exception e) {
            showToast("输入正确的金额");
            return;
        }
        if (!TextUtils.isEmpty(address.getText().toString())) {
            checkModel.setAddress(address.getText().toString());
        }
        checkModel.setType(getSelectType());
        checkModel.setCreateTime(System.currentTimeMillis());
        CheckDao checkDao = new CheckDao(getActivity());
        checkDao.insert(checkModel);
        CheckTotalDao checkTotalDao = new CheckTotalDao(MyApplication.getAppContext());
        CheckTotalModel checkTotalModel = checkTotalDao.findOneRecord(year, month, day);
        if (checkTotalModel == null) {
            checkTotalModel = new CheckTotalModel();
        }
        if (mCheckModel != null && checkTotalModel.getMoney() > 0) {
            checkTotalModel.setMoney(checkTotalModel.getMoney() - mCheckModel.getMoney());
        }
        checkTotalModel.setMoney(checkTotalModel.getMoney() + checkModel.getMoney());
        checkTotalModel.setYear(year);
        checkTotalModel.setMonth(month);
        checkTotalModel.setDay(day);
        checkTotalDao.insert(checkTotalModel);

        Intent intent = new Intent("REFRESH_CHECK");
        LocalBroadcastManager.getInstance(MyApplication.getAppContext()).sendBroadcast(intent);
        getActivity().finish();
    }
}
